#include "FileManageCenter.h"
#include <QDateTime>
#include <string.h>
#include "clcomidfactory.h"
#include "clcomfuncs.h"

#define MAX_PATH 256

CFileManageCenter* CFileManageCenter::_Instance = nullptr;

CFileManageCenter* CFileManageCenter::GetInstance()
{
	if (!_Instance)
	{
		_Instance = new CFileManageCenter;
	}
	return _Instance;
}
void CFileManageCenter::Release()
{
    if (_Instance != nullptr)
    {
        delete _Instance;
        _Instance = nullptr;
    }
}
CFileManageCenter::CFileManageCenter()
{
}


CFileManageCenter::~CFileManageCenter()
{
}

/*begin:xml*/
int CFileManageCenter::XMLCreateFile(const char* filename, char* czID)
{
	if (FM_RET_VALUE_OK == XMLOpenFile(filename, czID))
	{
		return FM_RET_VALUE_OK;
	}
	
	string fileid = "";
    string filenamestr = filename;
    _mapFileNameFileID.GetData(filenamestr, fileid);
    if (fileid.length() > 0)
	{
        sprintf(czID, "%s", fileid.c_str());

		return FM_RET_VALUE_OK;
	}
	memset((void*)czID, 0, F_ID_LEN);
    string id = CLComIDFactory::CreateID();
    CParseXMLConfigFile* pf = new CParseXMLConfigFile(id);

    pf->CreateXMLFile(filename);
    _mapIDXMLFiles.Add2Map(pf, pf->GetID());
	_mapFileNameFileID.Add2Map(pf->GetID(), filename);
    sprintf(czID, "%s", pf->GetID().c_str());
	return FM_RET_VALUE_OK;
}
int CFileManageCenter::XMLOpenFile(const char* fileName, char* czID)
{
	string fileid = "";
	if (CTASKMAP_OK == _mapFileNameFileID.GetData(fileid, fileName))
	{
        sprintf(czID, "%s", fileid.c_str());

		return FM_RET_VALUE_OK;
	}
	memset((void*)czID, 0, F_ID_LEN);
    string id = CLComIDFactory::CreateID();
    CParseXMLConfigFile* pf = new CParseXMLConfigFile(id);

    if (PXML_OK != pf->LoadXMLFile(fileName))
	{
        delete pf;
		return FM_RET_VALUE_FALSE;
	}

	_mapIDXMLFiles.Add2Map(pf, id);
	_mapFileNameFileID.Add2Map(pf->GetID(), fileName);
    sprintf(czID, "%s", id.c_str());
	return FM_RET_VALUE_OK;
}
int CFileManageCenter::XMLGetNodeByFile(XMLFILE pFile, const char* nodeName, char* czID)
{
	memset((void*)czID, 0, F_ID_LEN);
    CParseXMLConfigFile* pf = NULL;
	if (CTASKMAP_OK != _mapIDXMLFiles.GetData(pf, pFile))
	{
		return FM_RET_VALUE_FALSE;
	}

    PTREE pt = pf->GetNodeByName(nodeName);
	if (!pt)
	{
		return FM_RET_VALUE_FALSE;
	}
    sprintf(czID, "%s", pt->GetID().c_str());
	return FM_RET_VALUE_OK;
}
int CFileManageCenter::XMLGetNodeByFatherNode(
	XMLFILE pFile, 
	XMLTREE pFathertree, 
	const char* childNodeName, char* czID)
{
	memset((void*)czID, 0, F_ID_LEN);
    CParseXMLConfigFile* pf = NULL;
	if (CTASKMAP_OK != _mapIDXMLFiles.GetData(pf, pFile))
	{
		return FM_RET_VALUE_FALSE;
	}
	PTREE pt = pf->GetNodeByID(pFathertree);

	if (!pt)
	{
		return FM_RET_VALUE_FALSE;
	}

    PTREE ptt = pt->GetNodeByName(childNodeName);

	if (!ptt)
	{
		return FM_RET_VALUE_FALSE;
	}
    sprintf(czID, "%s", ptt->GetID().c_str());
	return FM_RET_VALUE_OK;
}

int CFileManageCenter::XMLGetNodeValueEx(XMLFILE pFile, const char*  fathernodename, const char*  nodename, XMLNODEINFO& outinfo)
{
	if (0 != strlen(fathernodename))
	{
		char czID[F_ID_LEN] = { 0 };
		if (FM_RET_VALUE_OK != XMLGetNodeByFile(pFile, fathernodename, czID))
		{
			if (FM_RET_VALUE_OK != XMLGetNodeByFile(pFile, nodename, czID))
			{
				return FM_RET_VALUE_FALSE;
			}
			return XMLGetNodeInfo(pFile, czID, outinfo);
		}
		char czSonID[F_ID_LEN] = { 0 };
		if (FM_RET_VALUE_OK != XMLGetNodeByFatherNode(pFile, czID, nodename, czSonID))
		{
			return FM_RET_VALUE_FALSE;
		}
		return XMLGetNodeInfo(pFile, czSonID, outinfo);
	}
	else
	{
		char czID[F_ID_LEN] = { 0 };
		if (FM_RET_VALUE_OK != XMLGetNodeByFile(pFile, nodename, czID))
		{
			return FM_RET_VALUE_FALSE;
		}
		return XMLGetNodeInfo(pFile, czID, outinfo);
	}
}
int CFileManageCenter::XMLSetNodeValueEx(XMLFILE pFile, const char*  fathernodename, const char*  nodename, XMLNODEINFO& outinfo)
{
	char czFID[F_ID_LEN] = { 0 };
	if (0 != strlen(fathernodename))
	{
		if (FM_RET_VALUE_OK != XMLGetNodeByFile(pFile, fathernodename, czFID))
		{
			XMLNODEINFO fahternodeinfo;
            sprintf(fahternodeinfo.czName, "%s", fathernodename);

			XMLAddNode(pFile, "", fahternodeinfo);

			if (FM_RET_VALUE_OK != XMLGetNodeByFile(pFile, fathernodename, czFID))
			{
				return FM_RET_VALUE_FALSE;
			}
		}
	}
	else
	{
		if (FM_RET_VALUE_OK != XMLGetRootNode(pFile, czFID))
		{
			return FM_RET_VALUE_FALSE;
		}
	}
	char czID[F_ID_LEN] = { 0 };

	if (FM_RET_VALUE_OK != XMLGetNodeByFatherNode(pFile, czFID, nodename, czID))
	{
		return XMLAddNode(pFile, czFID, outinfo);
	}
	return XMLSetNodeValue(pFile, czFID, outinfo);
}
int CFileManageCenter::XMLModifyNodeAttribute(XMLFILE pFile, XMLTREE ptree, const char* attributeName, const char* attributeValue)
{
    CParseXMLConfigFile* pf = NULL;
	if (CTASKMAP_OK != _mapIDXMLFiles.GetData(pf, pFile))
	{
		return FM_RET_VALUE_FALSE;
	}
	PTREE pt = pf->GetNodeByID(ptree);

	if (!pt)
	{
		return FM_RET_VALUE_FALSE;
	}

    return pt->UpdateAttribute(attributeName, attributeValue);
}
int CFileManageCenter::XMLGetNodeInfo(XMLFILE pFile, XMLTREE ptree, XMLNODEINFO& value)
{
    CParseXMLConfigFile* pf = NULL;
	if (CTASKMAP_OK != _mapIDXMLFiles.GetData(pf, pFile))
	{
		return FM_RET_VALUE_FALSE;
	}
	PTREE pt = pf->GetNodeByID(ptree);

	if (!pt)
	{
		return FM_RET_VALUE_FALSE;
	}
    memcpy(value.czName, pt->GetName().toLatin1().data(), pt->GetName().length());
    memcpy(value.czValue, pt->GetValue().toLatin1().data(), pt->GetValue().length());
    memcpy(value.czAttribute, pt->GetAttru().toLatin1().data(), pt->GetAttru().length());

	switch (value.useType)
	{
	case FM_VALUE_TYPE_NO:
		return FM_RET_VALUE_FALSE;
	case FM_VALUE_TYPE_INT:
		value.nValue = pt->GetIntValue();
		return FM_RET_VALUE_OK;
	case FM_VALUE_TYPE_DOUBLE:
		value.dValue = pt->GetDoubleValue();
		return FM_RET_VALUE_OK;
	case FM_VALUE_TYPE_CHARS:
    {
                                 sprintf(value.czValue, "%s", pt->GetValue().toLatin1().data());
								 return FM_RET_VALUE_OK;
	}
	default:
		break;
	}

	return FM_RET_VALUE_OK;
}
int CFileManageCenter::XMLGetValue(XMLFILE pFile, const char*  aName, XMLNODEINFO& outinfo)
{
    CParseXMLConfigFile* pf = NULL;
	if (CTASKMAP_OK != _mapIDXMLFiles.GetData(pf, pFile))
	{
		return FM_RET_VALUE_FALSE;
	}
	PTREE pp = pf->GetNodeByName(aName);
	if (!pp)
	{
		return FM_RET_VALUE_FALSE;
	}
    memcpy(outinfo.czName, pp->GetName().toLatin1().data(), pp->GetName().length());
    memcpy(outinfo.czValue, pp->GetValue().toLatin1().data(), pp->GetValue().length());
    memcpy(outinfo.czAttribute, pp->GetAttru().toLatin1().data(), pp->GetAttru().length());

	switch (outinfo.useType)
	{
	case FM_VALUE_TYPE_NO:
		return FM_RET_VALUE_FALSE;
	case FM_VALUE_TYPE_INT:
		outinfo.nValue = pp->GetIntValue();
		return FM_RET_VALUE_OK;
	case FM_VALUE_TYPE_DOUBLE:
		outinfo.dValue = pp->GetDoubleValue();
		return FM_RET_VALUE_OK;
	case FM_VALUE_TYPE_CHARS:
	{

                                 sprintf(outinfo.czValue, "%s", pp->GetValue().toLatin1().data());

								 return FM_RET_VALUE_OK;
	}
	default:
		break;
	}
	return FM_RET_VALUE_FALSE;
}
QString TransValueStr(XMLNODEINFO& value)
{
    QString valuebuf = "";
    switch (value.useType)
    {
    case FM_VALUE_TYPE_NO:
        return "";
    case FM_VALUE_TYPE_INT:
        valuebuf = QString("%1").arg(value.nValue);
        break;
    case FM_VALUE_TYPE_DOUBLE:
        valuebuf = QString("%1").arg(value.dValue);
        break;
    case FM_VALUE_TYPE_CHARS:
        valuebuf = QString("%1").arg(value.czValue);
        break;
    default:
        break;
    }
    return valuebuf;
}
int CFileManageCenter::XMLSetNodeValue(XMLFILE pFile, const char*  aName, XMLNODEINFO& ininfo)
{
    CParseXMLConfigFile* pf = NULL;
	if (CTASKMAP_OK != _mapIDXMLFiles.GetData(pf, pFile))
	{
		return FM_RET_VALUE_FALSE;
	}
    QString valuebuf = TransValueStr(ininfo);
    if (valuebuf.length() == 0)
    {
        return FM_RET_VALUE_FALSE;
    }
	PTREE pp = pf->GetNodeByName(aName);
	if (!pp)
	{
        PTREE proot = pf->GetNodeByName("root");
		if (proot)
		{
            proot->AddChild(aName, valuebuf);
		}
	}
	else
    {
        pp->SetValue(valuebuf);
	}

	pf->Save2XMLFile();

	return FM_RET_VALUE_OK;
}
int CFileManageCenter::XMLSetNodeValueEx(XMLFILE pFile, XMLTREE ptree, XMLNODEINFO& value)
{
    CParseXMLConfigFile* pf = NULL;
	if (CTASKMAP_OK != _mapIDXMLFiles.GetData(pf, pFile))
	{
		return FM_RET_VALUE_FALSE;
	}
	PTREE pt = pf->GetNodeByID(ptree);

	if (!pt)
	{
		return FM_RET_VALUE_FALSE;
	}
    QString valuebuf = TransValueStr(value);
    if (valuebuf.length() == 0)
    {
        return FM_RET_VALUE_FALSE;
    }

    pt->SetValue(valuebuf);

	return FM_RET_VALUE_OK;
}
int CFileManageCenter::XMLDelNode(
	XMLFILE pFile,
	XMLTREE ptree)
{
    CParseXMLConfigFile* pf = NULL;
	if (CTASKMAP_OK != _mapIDXMLFiles.GetData(pf, pFile))
	{
		return FM_RET_VALUE_FALSE;
	}
	PTREE pt = pf->GetNodeByID(ptree);

	if (!pt)
	{
		return FM_RET_VALUE_FALSE;
	}
	PTREE ptf = pt->GetFather();

	if (!ptf)
	{
		pf->DelPtree(pt->GetID());
	}
	else
	{
		ptf->DelNodeByName(pt->GetName());
	}

	return FM_RET_VALUE_OK;
}
int CFileManageCenter::XMLAddNode(XMLFILE pFile, XMLTREE pFathertree, XMLNODEINFO& value)
{
    CParseXMLConfigFile* pf = NULL;
	if (CTASKMAP_OK != _mapIDXMLFiles.GetData(pf, pFile))
	{
		return FM_RET_VALUE_FALSE;
	}
	PTREE pfather = pf->GetNodeByID(pFathertree);

	if (!pfather && 0 == strlen(pFathertree))
	{
		pfather = pf->GetRootNode();
	}
	if (!pfather)
	{
		return FM_RET_VALUE_FALSE;
	}

    QString valuebuf = TransValueStr(value);
    if (valuebuf.length() == 0)
    {
        return FM_RET_VALUE_FALSE;
    }
    pfather->AddChild(value.czName, valuebuf);

	return FM_RET_VALUE_OK;
}

int CFileManageCenter::XMLGetFahterNode(XMLFILE pFile, XMLTREE pChildtree, char* czID)
{
	memset((void*)czID, 0, F_ID_LEN);
    CParseXMLConfigFile* pf = NULL;
	if (CTASKMAP_OK != _mapIDXMLFiles.GetData(pf, pFile))
	{
		return FM_RET_VALUE_FALSE;
	}
	PTREE pt = pf->GetNodeByID(pChildtree);

	if (!pt)
	{
		return FM_RET_VALUE_FALSE;
	}
	PTREE pfather = pt->GetFather();
	if (!pfather)
	{
		return FM_RET_VALUE_FALSE;
	}
    sprintf(czID, "%s", pfather->GetID().c_str());
	return FM_RET_VALUE_OK;
}
int CFileManageCenter::XMLGetChildNodes(
	XMLFILE pFile, 
	XMLTREE ptree, 
	vector<string>& list)
{
    CParseXMLConfigFile* pf = NULL;
	if (CTASKMAP_OK != _mapIDXMLFiles.GetData(pf, pFile))
	{
		return FM_RET_VALUE_FALSE;
	}
	PTREE pt = pf->GetNodeByID(ptree);

	if (!pt)
	{
		return FM_RET_VALUE_FALSE;
	}
	std::map<string, CXMLTree*>::iterator it;
	PTREE pchild = pt->GetFirstNode(it);
	while (pchild)
	{
		string tempstr = it->second->GetID();
		list.push_back(tempstr);

		pchild = pt->GetNextNode(it);
	}

	return FM_RET_VALUE_OK;
}

int CFileManageCenter::XMLGetRootNode(XMLFILE pFile, char* czID)
{
	memset((void*)czID, 0, F_ID_LEN);
    CParseXMLConfigFile* pf = NULL;
	if (CTASKMAP_OK != _mapIDXMLFiles.GetData(pf, pFile))
	{
		return FM_RET_VALUE_FALSE;
	}
	PTREE pt = pf->GetRootNode();
    sprintf(czID, "%s", pt->GetID().c_str());
	return FM_RET_VALUE_OK;
}

int CFileManageCenter::XMLSaveXMLFile(XMLFILE pFile)
{
    CParseXMLConfigFile* pf = NULL;
	if (CTASKMAP_OK != _mapIDXMLFiles.GetData(pf, pFile))
	{
		return FM_RET_VALUE_FALSE;
	}
	return pf->Save2XMLFile();
}

void CFileManageCenter::XMLCloseFile(XMLFILE pFile)
{
    CParseXMLConfigFile* pf = NULL;
	if (CTASKMAP_OK != _mapIDXMLFiles.GetData(pf, pFile))
	{
		return ;
	}
    string filename = pf->GetConfigFileName().toLatin1().data();

    _mapFileNameFileID.EraseData(filename);

    string idstr = pFile;
    _mapIDXMLFiles.EraseDataAndReleaseHeap(idstr);
}
/*end:xml*/

/*begin:SELF FILE*/
int CFileManageCenter::SELFFCreateFile(
        const char* inFilename,
        char* czID,
        bool bCreate)
{
    PSELFFORMARTFILE pf = new SELFFORMARTFILE(
                inFilename, CLComIDFactory::CreateID(), bCreate);

	if (pf->GetID() == "")
	{
		return FM_RET_VALUE_FALSE;
	}
	
    sprintf(czID, "%s", pf->GetID().c_str());
	_mapIDSELFFFiles.Add2Map(pf, pf->GetID());
	return FM_RET_VALUE_OK;
}
void CFileManageCenter::SELFFCLOSEFile(const char* fid)
{
	PSELFFORMARTFILE p = NULL;
	if (CTASKMAP_OK != _mapIDSELFFFiles.GetData(p, fid))
	{
		return;
	}
	p->CloseFile();
    string idstr = fid;
    _mapIDSELFFFiles.EraseDataAndReleaseHeap(idstr);
}
bool CFileManageCenter::SELFFBFileValid(const char* fid)
{
	PSELFFORMARTFILE p = NULL;
	if (CTASKMAP_OK != _mapIDSELFFFiles.GetData(p, fid))
	{
        return false;
	}
	return p->BFileValid();
}
int CFileManageCenter::SELFFReadFile(const char* fid, void* readContent, int contentLen)
{
	PSELFFORMARTFILE p = NULL;
	if (CTASKMAP_OK != _mapIDSELFFFiles.GetData(p, fid))
	{
        return false;
	}
	return p->ReadFile(readContent, contentLen);
}
int CFileManageCenter::SELFFReadLine(const char* fid, void* readcontent, int contentLen)
{
	PSELFFORMARTFILE p = NULL;
	if (CTASKMAP_OK != _mapIDSELFFFiles.GetData(p, fid))
	{
        return false;
	}
	return p->ReadLine(readcontent, contentLen);
}
bool CFileManageCenter::SELFFReadOver(const char* fid)
{
	PSELFFORMARTFILE p = NULL;
	if (CTASKMAP_OK != _mapIDSELFFFiles.GetData(p, fid))
	{
        return false;
	}
	return p->ReadOver();
}
int CFileManageCenter::SELFFRWriteFile(const char* fid, void* writeStr, int nWriteSize)
{
	PSELFFORMARTFILE p = NULL;
	if (CTASKMAP_OK != _mapIDSELFFFiles.GetData(p, fid))
	{
        return false;
	}
	return p->RWriteFile(writeStr, nWriteSize);
}
int CFileManageCenter::SELFFAppendWriteFile(const char* fid, void* writeStr, int nWriteSize)
{
	PSELFFORMARTFILE p = NULL;
	if (CTASKMAP_OK != _mapIDSELFFFiles.GetData(p, fid))
	{
        return false;
	}
	return p->AppendWriteFile(writeStr, nWriteSize);
}
void CFileManageCenter::SELFFSetEncrypt(const char* fid, bool bEncrypt)
{
	PSELFFORMARTFILE p = NULL;
	if (CTASKMAP_OK != _mapIDSELFFFiles.GetData(p, fid))
	{
		return;
	}
	return p->SetEncrypt(bEncrypt);
}
void CFileManageCenter::SELFFSetFileName(const char* fid, const char* filename)
{
	PSELFFORMARTFILE p = NULL;
	if (CTASKMAP_OK != _mapIDSELFFFiles.GetData(p, fid))
	{
		return;
	}
	return p->SetFileName(filename);
}
void CFileManageCenter::SELFFGetFileName(const char* fid, char* outfilename, int nBuffersize)
{
	PSELFFORMARTFILE p = NULL;
	if (CTASKMAP_OK != _mapIDSELFFFiles.GetData(p, fid))
	{
		return;
	}
	return p->GetFileName(outfilename, nBuffersize);
}
int CFileManageCenter::SELFFGetFileSize(const char* fid)
{
	PSELFFORMARTFILE p = NULL;
	if (CTASKMAP_OK != _mapIDSELFFFiles.GetData(p, fid))
	{
        return false;
	}
	return p->GetFileSize();
}
/*begin:SELF FILE*/

/*begin:cad*/
FM_CAD_FILE_TYPE CFileManageCenter::ParseCADTypeByName(const char* filename)
{
    QString filenames = filename;

    if (filenames.contains("csv", Qt::CaseInsensitive))
    {
        return FM_CAD_FILE_TYPE_CSV;
    }
    else if (filenames.contains("txt", Qt::CaseInsensitive))
    {
        return FM_CAD_FILE_TYPE_TXT;
    }
    else if (filenames.contains("cad", Qt::CaseInsensitive))
    {
        return FM_CAD_FILE_TYPE_CAD;
    }
    return FM_CAD_FILE_TYPE_NO;
}
int CFileManageCenter::CADCreateFile(const char* inFilename, char* czID)
{
	memset((void*)czID, 0, F_ID_LEN);
    PPARSECADFILE pf = new CCADFile(inFilename, CLComIDFactory::CreateID());

	_mapIDCADFiles.Add2Map(pf, pf->GetID());
    sprintf(czID, "%s", pf->GetID().c_str());
	return FM_RET_VALUE_OK;
}
int CFileManageCenter::CADOpenFile(const char* inFileName, char* czID)
{
	memset((void*)czID, 0, F_ID_LEN);
	string id = "";
    PPARSECADFILE pf = new CCADFile(inFileName, CLComIDFactory::CreateID());
	FM_CAD_FILE_TYPE type = ParseCADTypeByName(inFileName);
	if (type == FM_CAD_FILE_TYPE_CAD)
	{
		if (FM_RET_VALUE_OK != pf->ParseCADFile(inFileName))
		{
            delete pf;
            pf = nullptr;
			return FM_RET_VALUE_FALSE;
		}
	}
	else if (type == FM_CAD_FILE_TYPE_CSV)
	{
		if (FM_RET_VALUE_OK != pf->ParseCSVFile(inFileName))
		{
            delete pf;
            pf = nullptr;
			return FM_RET_VALUE_FALSE;
		}
	}
	else if (type == FM_CAD_FILE_TYPE_TXT)
	{
		if (FM_RET_VALUE_OK != pf->ParseTXTFile(inFileName))
		{
            delete pf;
            pf = nullptr;
			return FM_RET_VALUE_FALSE;
		}
	}
	else
	{
        delete pf;
        pf = nullptr;
		return FM_RET_VALUE_FALSE;
	}
	_mapIDCADFiles.Add2Map(pf, pf->GetID());
    sprintf(czID, "%s", pf->GetID().c_str());
	return FM_RET_VALUE_OK;
}
int CFileManageCenter::CADGetColumns(const char* fid, CADCOLUMNLINE& outLineColumn)
{
	PPARSECADFILE p = NULL;

	if (CTASKMAP_OK != _mapIDCADFiles.GetData(p, fid))
	{
		return FM_RET_PARAMETER_ERROR;
	}
	p->GetLineColumnCount(outLineColumn);
	return FM_RET_VALUE_OK;
}
int CFileManageCenter::CADGetData(const char* fid, CADCOLUMNLINE& inLineColumn, FM_VALUE_TYPE inType, CADVALUEINFO& outValue)
{
	PPARSECADFILE p = NULL;

	if (CTASKMAP_OK != _mapIDCADFiles.GetData(p, fid))
	{
		return FM_RET_PARAMETER_ERROR;
	}
	return p->GetValue(inLineColumn, inType, outValue);
}

int CFileManageCenter::CADAddData(const char* fid, CADCOLUMNLINE& inLineColumn, FM_VALUE_TYPE inType, CADVALUEINFO Value)
{
	PPARSECADFILE p = NULL;

	if (CTASKMAP_OK != _mapIDCADFiles.GetData(p, fid))
	{
		return FM_RET_PARAMETER_ERROR;
	}
	return p->SetValue(inLineColumn, inType, Value);
}

void CFileManageCenter::CADCloseFile(const char* fid)
{
	PPARSECADFILE p = NULL;

	if (CTASKMAP_OK != _mapIDCADFiles.GetData(p, fid))
	{
		return;
	}
	p->ResetFile();

    string fidstr = fid;
    _mapIDCADFiles.EraseDataAndReleaseHeap(fidstr);
}
int CFileManageCenter::CADSaveFile(const char* fid, const char* filename)
{
	PPARSECADFILE pf = NULL;
	FM_CAD_FILE_TYPE type = ParseCADTypeByName(filename);
	if (CTASKMAP_OK != _mapIDCADFiles.GetData(pf, fid))
	{
		return FM_RET_PARAMETER_ERROR;
	}
	if (type == FM_CAD_FILE_TYPE_CAD)
	{
		if (FM_RET_VALUE_OK != pf->Save2CADFile(filename))
		{
			return FM_RET_PARAMETER_ERROR;
		}
	}
	else if (type == FM_CAD_FILE_TYPE_CSV)
	{
		if (FM_RET_VALUE_OK != pf->Save2CSVFile(filename))
		{
			return FM_RET_PARAMETER_ERROR;
		}
	}
	else if (type == FM_CAD_FILE_TYPE_TXT)
	{
		if (FM_RET_VALUE_OK != pf->Save2TXTFile(filename))
		{
			return FM_RET_PARAMETER_ERROR;
		}
	}
	else
	{
		return FM_RET_PARAMETER_ERROR;
	}

	return FM_RET_VALUE_OK;
}
/*end:cad*/
